package com.xca.is.adapter;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xca.is.R;
import com.xca.is.entity.TongYi;

import java.util.List;

public class TongYiAdapter extends RecyclerView.Adapter<TongYiAdapter.ViewHolder>{
    private static final int TYPE_HEADER = 0 ;
    private static final int TYPE_CONTENT = 1 ;
    private List<TongYi> list ;
    private Context context ;
    private Activity activity ;
    private int currentLength = 0 ;

   /* private TextRevealer textRevealer ;
   {
   textRevealer = new TextRevealer(t1, text, 50);
   textRevealer.startRevealingText();
   }
    private Handler handler = new Handler(Looper.getMainLooper());*/

    public TongYiAdapter(List<TongYi> list, Context context, Activity activity) {
        this.list = list;
        this.context = context;
        this.activity = activity;
    }
    public void updateList(List<TongYi> list){

        this.list = list ;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_tongyi,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TongYi tongYi = list.get(position);
        Log.d("传递过来的数据", tongYi.getText());
        if (tongYi.getType()==TYPE_HEADER){
            holder.l1.setVisibility(View.VISIBLE);
            holder.l2.setVisibility(View.GONE);
            holder.t1.setText(tongYi.getText());

           // showText(holder.t1,tongYi.getText());
        }else {
            holder.l1.setVisibility(View.GONE);
            holder.l2.setVisibility(View.VISIBLE);
            holder.t2.setText(tongYi.getText());
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

private void showText(TextView view, String text) {
    if (text == null) {
        return; // 处理空值情况
    }

    ValueAnimator animator = ValueAnimator.ofInt(0, text.length());
    animator.setDuration(50 * text.length()); // 每个字符显示间隔 50 毫秒
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            int currentLength = (int) animation.getAnimatedValue();
            view.setText(text.substring(0, currentLength));
        }
    });

    animator.start(); // 启动动画


    /* -------------------------------------------------------------
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (currentLength < text.length()) {
                view.setText(text.substring(0, ++currentLength));
                handler.postDelayed(this, 50);
            } else {
                handler.removeCallbacks(this); // 移除回调，避免内存泄漏
            }
        }
    };

    handler.post(runnable); // 启动 Runnable*/
}


    static class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout l1 , l2 ;
        TextView t1 , t2 ;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            l1 = itemView.findViewById(R.id.tyl1) ;
            l2 = itemView.findViewById(R.id.tyl2) ;
            t1 = itemView.findViewById(R.id.tyt1) ;
            t2 = itemView.findViewById(R.id.tyt2) ;
        }
    }
}
